<?php
use \Model\User;

/**
 * The Login Controller.
 *
 * Login portal for users
 * 
 * @package  app
 * @extends  Controller
 */
class Controller_Login extends Controller
{
	public function action_portal() {
		$login = Session::get('login');
		if ($login) {
			Response::redirect('bind');
		}
		return Response::forge(View::forge('login/portal'));
	}

	public function action_check() {
		$ret = User::is_valid(Input::post('username', ''), Input::post('password', ''));
		if ($ret) {
			Session::set('username', Input::post('username', ''));
			$role = User::get_role(Input::post('username', ''), Input::post('password', ''));
			$user_id = User::get_id(Input::post('username', ''), Input::post('password', ''));
			Session::set('role', $role);
			Session::set('user_id', $user_id);
			Session::set('login', true);
		}
		$msg = $ret ? 'succ' : 'fail';
		return json_encode(array('msg' => $msg));
	}
	
	public function action_logout() {
		Session::set('login', false);
		Response::redirect(Uri::base(false) . 'login');
	}
}
